#coding=utf-8
'''
Created on 2010-9-20
@author: lxd
'''
import sys
sys.path.append('.')
sys.path.append('..')
import wx
import threading
import web
import wxHelper
import config
import tools

class YesThread(threading.Thread): 
    def __init__(self,window,log):
        threading.Thread.__init__(self)
        self.window = window
        self.log = log
        self.web = web.SimpleWeb(log)
        
    def run(self):
        email = self.window.txtEmail.GetLabel().strip()
        pwd = self.window.txtPwd.GetLabel().strip()
        key = tools.getKeyOfEmail(email)

        data = dict(email = email,pwd = pwd,key = key)
        url = '/queryperson'
        f = self.web.fetchData(config.web_root+url,data)
        wx.CallAfter(self.window.yesCall, f)

class MainPanel(wx.Panel):
    def __init__(self, parent, log, **kargs):
        wx.Panel.__init__(self, parent)
        self.log = log

        self.lblEmail,self.txtEmail= wxHelper.getLblAndTxt(self,u'邮箱*')
        self.lblPwd,self.txtPwd = wxHelper.getLblAndTxt(self,u'密码*',True)
        
        self.btnYes= wx.Button(self, -1, u'查询')
        self.Bind(wx.EVT_BUTTON, self.OnYesClick, self.btnYes)  
        
        self.layout()
        
    def layout(self):
        userSizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5)
        userSizer.AddGrowableCol(1)
        userSizer.Add(self.lblEmail, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        userSizer.Add(self.txtEmail, 0, wx.EXPAND)
        userSizer.Add(self.lblPwd, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        userSizer.Add(self.txtPwd, 0, wx.EXPAND)
        
        topBox = wx.StaticBox(self, -1, u'查询')
        topSizer = wx.StaticBoxSizer(topBox, wx.VERTICAL)
        topSizer.Add(userSizer, 0, wx.EXPAND|wx.ALL, 5)
        
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(topSizer, 0, wx.EXPAND|wx.ALL, 5)
        mainSizer.Add(self.btnYes, 0, wx.ALIGN_CENTRE|wx.TOP|wx.BOTTOM, 5)
        self.SetSizer(mainSizer)

    def yes(self):
        self.yesThread = YesThread(self,self.log)
        self.yesThread.setDaemon(True)
        self.yesThread.start()

    def OnYesClick(self, event): 
        def isValid():
            if wxHelper.isBlank(self.txtEmail):return False
            if wxHelper.isBlank(self.txtPwd):return False
            if wxHelper.isNotEmail(self.txtEmail):return False
            return True

        if isValid():
            self.btnYes.Enabled = False
            self.yes()

    def yesCall(self,f):
        self.btnYes.Enabled = True
        data = f.read()
        msg = config.msg.get(data,data)
        wxHelper.msg(msg)
                 
if __name__ == '__main__':
    import config
    app = wx.PySimpleApp()
    mainFrame = wx.Frame(None, -1, 'main', size=(800, 600))
    MainPanel(mainFrame, config.log)
    mainFrame.Show()
    app.MainLoop()    

